//
//  DWAutoReleasePoolViewController.m
//  OCAPPDemo
//
//  Created by 杨冬武 on 2021/4/21.
//

#import "DWAutoReleasePoolViewController.h"
#import "DWMRCView.h"
#import <objc/runtime.h>
#import "DWView.h"

extern void _objc_autoreleasePoolPrint(void);

@interface DWAutoReleasePoolViewController () {
    id __weak _obj;
}

@property (nonatomic, weak) DWMRCView *arcView;

@property (nonatomic, weak) id obj;

@end

@implementation DWAutoReleasePoolViewController

int b = 0;
int b1;

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self test];
}


- (void)test {
    NSString *s = @"a";
    static int a1;
    static int a = 0;
    
    NSObject *v0 = [NSObject new];
    Class c0 = object_getClass(v0);
    Class m0 = object_getClass(c0);
    
    UIView *v = [UIView new];
    Class c = object_getClass(v);
    Class m = object_getClass(c);
    
    DWView *v1 = [DWView new];
    Class c1 = object_getClass(v1);
    Class m1 = object_getClass(c1);
    
    int t = 0;
    int fff = 1;
    
    NSLog(@"字符串常量: %p", s);
    NSLog(@"初始化的: %p-%p", &a, &b);
    NSLog(@"未初始化的: %p-%p", &a1, &b1);
    NSLog(@"NSObject: %p-%p-%p", v0, c0, m0);
    NSLog(@"UIView: %p-%p-%p", v, c, m);
    NSLog(@"DWView: %p-%p-%p", v1, c1, m1);
    NSLog(@"局部变量: %p-%p", &t, &fff);
}


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    //NSLog(@"viewWillAppear %@", self.arcView);
    
    /*
    SEL sel;
    NSMethodSignature *s = [self methodSignatureForSelector:_cmd];
    IMP imp;
     */
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
    //NSLog(@"viewDidAppear %@", self.arcView);
    
}

- (void)testMemoryClassObject {
    NSString *s = @"a";
    static int a1;
    static int a = 0;
    UIView *v = [UIView new];
    Class c = object_getClass(v);
    Class m = object_getClass(c);
    
    DWMRCView *v1 = [DWMRCView new];
    Class c1 = object_getClass(v);
    Class m1 = object_getClass(c);
    
    int t = 0;
    
    NSLog(@"%p", s);
    NSLog(@"%p-%p", c, m);
    NSLog(@"%p-%p", &a, &b);
    NSLog(@"%p-%p", &a1, &b1);
    NSLog(@"%p", v);
    
    NSLog(@"%p-%p-%p", v1, c1, m1);
    NSLog(@"%p", &t);
}

- (IBAction)printPoolStatus:(id)sender {
    
//    for (int i = 0; i < 50; ++i) {
//        NSArray *array = [NSArray array];
//    }
    _objc_autoreleasePoolPrint();
    
}

- (IBAction)check:(id)sender {
    
    
    for (int i = 0; i < 10000; i++) {
        __autoreleasing NSObject *obj = [[NSObject alloc] init];
    }
    
    _objc_autoreleasePoolPrint();
}


- (void)autoreleaseObject {
    __autoreleasing DWMRCView *mview = [[DWMRCView alloc] init];
    self.arcView = mview;
}


- (void)memoryAlign {
    
    struct Struct03 {
        char e;
        int f;
        short g;
        double h;
    };
    
    struct Struct02 {
        char e;
        int f;
        short g;
        double h;
        struct Struct03 s;
    };
    
    struct Struct01 {
        char a;
        double b;
        char c;
        short d;
        struct Struct02 s;
    };
    
    struct Struct01 a = {0};
    struct Struct02 b = {0};
    struct Struct03 c = {0};
    
    NSLog(@"size a = %lu, size b = %lu, size c = %lu", sizeof(a), sizeof(b), sizeof(c));
    NSLog(@"a = %p, s1 = %p, s2 = %p", &a, &(a.s), &(a.s.s));
}


@end
